package com.fairytask.ui.mainUnit
{
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.text.Input;
	import com.fairytask.engine.data.Modulator;
	import com.fairytask.engine.data.View;
	import com.fairytask.engine.data.ViewObject;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ViewDefine;
	import com.fairytask.engine.managers.ResManager;
	import com.jzonl.stage.charactor.Player;
	import com.fairytask.ui.base.UnitUI;
	import com.fairytask.ui.item.LineupPetListItem;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	
	/**
	 * 角色和宠物的列表
	 * @author yangj
	 * 
	 */
	public class HeadListUI extends UnitUI
	{
		private static var _self		:HeadListUI;
		
		
		private const MAXNUM	:int = 6;	//单页显示的头像数量
		
		public var lastBtn		:Button;		//上一页
		public var nextBtn		:Button;		//下一页
		public var pageLab		:Input;			//页数
		
		private var _roleList	:Array = [];	//头像显示列表
		public var _petsData	:Array = [];	//宠物数据列表
		private var _model		:Modulator;
		
		private var _player	:Player		= Player.getInstance();
		private var _curSelect	:int	= -1;	//当前对象位置
		private var _curObj	:int	= -1;		//当前选择对象
		//分页显示数据
		private var _curPage	:int = 1;	//当前页
		private var _maxPage	:int = 1;	//最大页数
		private var _petCount	:int = 1;	//宠物数量，用于计算宠物总数
		
		//解锁总数
		private var _unbind	:Array	=	[];
		
		public static function getInstance():HeadListUI
		{
			if (_self == null)
			{
				_self=new HeadListUI;
			}
			return _self;
		}
		
		public function HeadListUI()
		{
			super();
			_model = Modulator.instance;
			initList();
			initBind();
			initEvent();
		}
		
		/**
		 * 初始化头像列表 
		 * 
		 */
		private function initList():void
		{
			for(var i:int=0;i<MAXNUM;i++)
			{
				var mc:MovieClip = new MovieClip();
				mc.pos = i;
				mc.mouseChildren = false;
				//间隔，位置
				mc.x = 15;
				mc.y = 20 + i * 74;
				mc.addEventListener(MouseEvent.CLICK,onClick);
				//给mc添加背景
				var bg:MovieClip = ResManager.newResClass("CustSkin_015") as MovieClip;
				bg.width = 65;
				bg.height = 65;
				mc.addChild(bg);
				//背景默认不显示
				bg.visible = false;
				windowBG.addChild(mc);
				_roleList.push(mc);
			}
			var item:LineupPetListItem	=	new LineupPetListItem(_player.data,true);
			_petsData[0] = item;
			
			//列表第一个位置是人物的
			//setPlayer();
		}
		
		/**
		 * 设置人物属性
		 * 
		 */
		private function setPlayer():void
		{
			//把人物加入位置
			//默认属性数据
			//onSelect(0);
		}
		
		/**
		 * 单击 
		 * @param evt
		 * 
		 */
		private function onClick(evt:MouseEvent):void
		{
			//不为空
			if(evt.currentTarget.data)
			{
				//选中当前对象
				onSelect(evt.currentTarget.pos);
			}
		}
		
		
		
		/**
		 * 选中对象 
		 * 
		 */
		private function onSelect(pos:int):void
		{
			
			if(_curSelect == pos)
			{
				return;
			}
			LineupUI.getInstance().removeAllSelect();
			if(_curSelect!=-1)
			{
				//先清除原先选中的滤镜
				_roleList[_curSelect].filters	=	null;
				var _cur	:MovieClip = _roleList[_curSelect] as MovieClip;
				var curObj:int;	
				if(_cur.data == _player.data)
				{
					curObj = 0;
				}
				else
				{
					var point:Point	= _cur.data.objId;
					//得到宠物的视图
					var view :View = model.scene.GetView(ViewDefine.VIEWPORT_PET);
					//把objId转化人字符串
					var str :String = point.toString();
					//得到对应的视图对象
					var obj:ViewObject = view.GetProperties(str);
					curObj = obj.GetProperties("Pos");
				}
				LineupUI.getInstance().removeSelect(curObj);
			}
			
			
			_curSelect = pos;
			var _curMc	:MovieClip = _roleList[pos] as MovieClip;
			//添加滤镜
			_curMc.filters	=	[Filter.YELLOW_INNER];
			//是人物
			if(_curMc.data == _player.data)
			{
				_curObj = 0;
			}
			else
			{
				var point1:Point	= _curMc.data.objId;
				//得到宠物的视图
				var view1 :View = model.scene.GetView(ViewDefine.VIEWPORT_PET);
				//把objId转化人字符串
				var str1 :String = point1.toString();
				//得到对应的视图对象
				var obj1:ViewObject = view1.GetProperties(str1);
				_curObj = obj1.GetProperties("Pos");
			}
			LineupUI.getInstance().select(_curObj);
		}
		
		/**
		 * 初始化绑定 
		 * 
		 */
		private function initBind():void
		{
			bindManager.BindView(model.scene,ViewDefine.VIEWPORT_PET,onAddPet,onRemovePet);
		}
		
		/**
		 * 初始化事件 
		 * 
		 */
		private function initEvent():void
		{
			
			lastBtn.addEventListener(MouseEvent.CLICK,onPageBack);
			nextBtn.addEventListener(MouseEvent.CLICK,onPageNext);
			
		}
		
		/**
		 * 上一页 
		 * @param evt
		 * 
		 */
		private function onPageBack(evt:MouseEvent):void
		{
			onPage(_curPage-1);
		}
		
		/**
		 * 下一页 
		 * @param evt
		 * 
		 */
		private function onPageNext(evt:MouseEvent):void
		{
			onPage(_curPage+1);
		}
		
		/**
		 * 显示第几页内容 
		 * @param page
		 * 
		 */
		private function onPage(page:int):void
		{
			LineupUI.getInstance().refresh();
			_curPage = page;
			//先清除当前容器内容
			clearList();
			pageLab.htmlStr = _curPage + "/" + _maxPage;
			//显示内容
			showPage();
			//按钮控制
			_curPage == 1 ? lastBtn.enabled = false : lastBtn.enabled = true;
			_curPage == _maxPage ? nextBtn.enabled = false : nextBtn.enabled = true;
			
		}
		
		

		/**
		 * 显示页面头像
		 * @param page 页数
		 * 
		 */
		private function showPage():void
		{
			
			refushPhoto();
		}
		
		
		/**
		 * 返回数据列表位置 
		 * @param pos 视图位置
		 * @return 
		 * 
		 */
		private function getPetPos(pos:int):int
		{
			return (_curPage-1)*MAXNUM + pos;
		}
		
		/**
		 * 返回当前页数量
		 * @param pos
		 * @return 
		 * 
		 */
		private function getLockedPets(val:int):int
		{
			var num	:int;
			//超过本页数量
			if(val >= _curPage * MAXNUM)
			{
				num = MAXNUM;
			}
				//不到当前页面
			else if(val <= (_curPage - 1) * MAXNUM)
			{
				num = 0;
			}
				//当前数量
			else
			{
				num = val % MAXNUM;
			}
			return num;
		}
		
		/**
		 * 添加宠物 
		 * @param key
		 * @param val
		 * @param data
		 * 
		 */
		private function onAddPet(key:String, val:ViewObject, data:Object):void
		{
			if(val.GetPropertiesEx("Model")=="")
			{
				return;
			}
			_petCount++;
			_unbind.push([val,"PetPos",bindManager.BindPropInViewObj(model.scene.GetView(ViewDefine.VIEWPORT_PET),key,"PetPos",onChangePos,null,[val,key])]);
			changeMaxPage();
		}
		
		
		/**
		 * 重新计算最大页面数量 
		 * 
		 */
		private function changeMaxPage():void
		{
			//重新计算最大页面
			_maxPage = Math.ceil(_petCount / MAXNUM);
			if(_maxPage > 1)
			{
				lastBtn.visible = true;
				nextBtn.visible = true;
				pageLab.visible = true;
				//页数x/xx
				pageLab.htmlStr = _curPage + "/" + _maxPage;
				//按钮控制
				_curPage == 1 ? lastBtn.enabled = false : lastBtn.enabled = true;
				_curPage == _maxPage ? nextBtn.enabled = false : nextBtn.enabled = true;
			}
			else
			{
				lastBtn.visible = false;
				nextBtn.visible = false;
				pageLab.visible = false;
			}
		}
		
		
		
		/**
		 * 更改位置的时候 
		 * @param key
		 * @param val
		 * @param arr
		 * 
		 */
		private function onChangePos(key:String,val:int,arr:Array):void
		{
			if(val<=0)
			{
				return;
			}
			//找到以前的数据进行删除
			for(var j:int=1;j<_petsData.length;j++)
			{
				var reItem:LineupPetListItem = _petsData[j] as LineupPetListItem;
				if(reItem == null)
				{
					continue;
				}
				if(reItem.data == arr[0])
				{
					_petsData[j] = null;
				}
			}
			var item:LineupPetListItem	=	new LineupPetListItem(arr[0]);
			_petsData[val] = item;
			refushPhoto();
		}
		
		
		/**
		 * 位置是否在当前页显示 
		 * @param pos
		 * @return 
		 * 
		 */
		private function isInCur(pos:int):Boolean
		{
			if(Math.floor(pos/ MAXNUM) == _curPage - 1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		/**
		 * 移除宠物 
		 * @param key
		 * @param val
		 * @param data
		 * 
		 */
		private function onRemovePet(key:String, val:ViewObject, data:Object):void
		{
			
			for(var j:int=1;j<_petsData.length;j++)
			{
				var item:LineupPetListItem = _petsData[j] as LineupPetListItem;
				if(item==null)
				{
					_petsData.splice(j,1);
				}
				else if(item.data == val)
				{
					_petsData.splice(j,1);
				}
				
				
			}
			_petCount--;
			var i	:int;
			for each(var itemBind:Array in _unbind)
			{
				if(itemBind[0] == val)
				{
					bindManager.UnbindProp(itemBind.shift(),itemBind.shift(),itemBind.shift());
					_unbind.splice(i,1);
					break;
				}
				i++;
			}
			changeMaxPage();
			refushPhoto();
		}
		
		
		/**
		 *刷新数据列表 
		 * 
		 */		
		private function refushPhoto():void
		{
			//先清除数据
			clearList();
			var id:int	=	(_curPage-1)*MAXNUM;
			for(var i:int =id;i<_petsData.length;i++)
			{
				//当不在当前页的时候就进行return
				if((i-id)>6)
				{
					return;
				}
				// 当数据是空的时候就进行跳出
				if(_petsData[i]==null)
				{
					continue;
				}
				addVGByIndex(_petsData[i],i-id)
			}
		}
		
		
		/**
		 * 添加头像图片及数据 
		 * @param val
		 * @param pos
		 * 
		 */
		private function addVGByIndex(val:LineupPetListItem,pos:int):void
		{
			//视图位置>数据错误
			if(pos >= 6)
			{
				return;
			}
			if(_roleList[pos].numChildren==2)
			{
				_roleList[pos].removeChildAt(1);
				
			}
			val.x = 6;
			val.y = 7;
			_roleList[pos].addChild(val);
			_roleList[pos].data = val.data;
			_roleList[pos].buttonMode = true;
			//背景显示
			_roleList[pos].getChildAt(0).visible = true;
		}
		
		/**
		 * 返回当前页的位置
		 * @param pos 数据位置
		 * @return 
		 * 
		 */
		private function getCurPos(pos:int):int
		{
			return pos%MAXNUM;
		}
		
		
		
		/**
		 * 清除头像列表，准备刷新 
		 * 
		 */
		private function clearList():void
		{
			for(var i:int=0;i<MAXNUM;i++)
			{
				(_roleList[i] as MovieClip).getChildAt(0).visible = false;
				if((_roleList[i] as MovieClip).numChildren==2)
				{
					(_roleList[i] as MovieClip).removeChildAt(1);
				}
			}
			//清除选中
			if(_curSelect!=-1)
			{
				//先清除原先选中的滤镜
				_roleList[_curSelect].filters	=	null;
			}
			_curSelect = -1;
			_curObj = -1;
			
		}
		
		/**
		 * 获取当前选择对象ID
		 * 
		 */
		public function get curObj():int
		{
			return _curObj;
		}
		
		/**
		 * 重置,默认都没有选中的效果
		 * 
		 */
		override public function reset():void
		{
			onPage(1);
			if(_curSelect!=-1)
			{
				//先清除原先选中的滤镜
				_roleList[_curSelect].filters	=	null;
			}
			_curSelect = -1
			_curObj	=	-1;
			//onSelect(0);
		}
		
		/**
		 *删除选中的效果 
		 * 
		 */		
		private function clearSelset():void
		{
			if(_curSelect!=-1)
			{
				//先清除原先选中的滤镜
				_roleList[_curSelect].filters	=	null;
			}
			_curSelect = -1
			_curObj	=	-1;
		}
		
		/**
		 *添加人物的出战 
		 * 
		 */		
		public function addFight(val:int):void
		{
			(_petsData[0] as LineupPetListItem).removFight();
		}
		
		/**
		 *删除人物的出战 
		 * 
		 */		
		public function removeFight(val:int):void
		{
			if((_roleList[val] as MovieClip).numChildren ==3)
			{
				(_roleList[val] as MovieClip).removeChildAt(2);
			}
		}
		
		/**
		 *删除所有的选中效果和所有的出战 
		 * 
		 */		
		public function removeAll():void
		{
			for(var i:int=1;i<_petsData.length;i++)
			{
				(_petsData[i] as LineupPetListItem).removFight();
			}
			clearSelset();
			
		}
		
		/**
		 *返回选中的位置 
		 * @return 
		 * 
		 */		
		public function get selectPos():int
		{
			return _curSelect;
		}
		
		/**
		 *当有阵型中有被选中的人物，给对应的人物头像添加选中的效果 
		 * 
		 */		
		public function addSelect(val:int):void
		{
			if(val==0)
			{
				if(_roleList[0].data == _player.data)
				{
					onSelect(0);
				}
				
			}
			
		}
		
		public function addpointSelect(val:Point):void
		{
			for(var i:int=0;i<_roleList.length;i++)
			{
				if(_roleList[i].data == _player.data)
				{
					continue;	
				}
				if((_roleList[i] as MovieClip).data.objId == val)
				{
					onSelect(i);
					break;
				}
			}	
		}
	}
}